package sivin.uikit.framework

import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import androidx.viewbinding.ViewBinding

open class BindingActivity<B : ViewBinding>(private val inflater: (LayoutInflater) -> B) :
    AppCompatActivity() {

    protected val mActivity: AppCompatActivity by lazy { this }

    protected val binding: B by lazy {
        inflater(layoutInflater)
    }

    protected open fun expandStatusBar(): Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            val top = if (expandStatusBar()) {
                0
            } else {
                systemBars.top
            }
            v.setPadding(systemBars.left, top, systemBars.right, systemBars.bottom)
            SystemUI.mStatusBarHeight = systemBars.top
            SystemUI.mNavigationBarHeight = systemBars.bottom
            insets
        }
        onViewCreate(savedInstanceState)
    }

    protected open fun onViewCreate(savedInstanceState: Bundle?) {
    }
}